<template>
  <div class="app-container">
    <!-- <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="治疗编号" prop="treatCode8">
        <el-input v-model="queryParams.treatCode8" placeholder="请输入治疗编号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="治疗了问诊人" prop="treatOpenid">
        <el-input v-model="queryParams.treatOpenid" placeholder="请输入治疗了问诊人" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="治疗备注" prop="treatRemark">
        <el-input v-model="queryParams.treatRemark" placeholder="请输入治疗备注" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="关联案例id" prop="treatCaseid">
        <el-input v-model="queryParams.treatCaseid" placeholder="请输入关联案例id" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="医生id" prop="treatDoctorid">
        <el-input v-model="queryParams.treatDoctorid" placeholder="请输入医生id" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="医生签名" prop="treatDoctorname">
        <el-input v-model="queryParams.treatDoctorname" placeholder="请输入医生签名" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="创建时间" prop="treatCreatetime">
        <el-date-picker clearable v-model="queryParams.treatCreatetime" type="date" value-format="yyyy-MM-dd"
          placeholder="请选择创建时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form> -->

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['ask:treat:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['ask:treat:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['ask:treat:remove']">删除</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['ask:treat:export']">导出</el-button>
      </el-col> -->
      <el-col :span="1.5" style="display: flex;">
        <div style=" padding-left: 40px;"> 问诊人:{{ caseInfo.caseUname}}</div>
        <div style=" padding-left: 40px;">问诊人手机号:{{ caseInfo.casePhone }}</div>
        <div style=" padding-left: 40px;">病历号:{{ caseInfo.caseCode6 }}</div>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-table v-loading="loading" :data="treatList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="治疗编号" align="center" prop="treatId" />
      <el-table-column label="治疗编号" align="center" prop="treatCode8" />
      <!-- <el-table-column label="治疗了问诊人" align="center" prop="treatOpenid" /> -->
      <el-table-column label="治疗详情" align="center" prop="treatText" />
      <el-table-column label="治疗备注" align="center" prop="treatRemark" />
      <el-table-column label="关联案例id" align="center" prop="treatCaseid" />
      <el-table-column label="医生id" align="center" prop="treatDoctorid" />
      <el-table-column label="医生签名" align="center" prop="treatDoctorname" />
      <el-table-column label="创建时间" align="center" prop="treatCreatetime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.treatCreatetime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['ask:treat:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['ask:treat:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改治疗管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <div style="display: flex; flex-direction: column; margin-bottom: 2vh; margin-left: 2.5vh;">
  <!-- 病历号和问诊人信息 -->
  <div style="font-size: 14px; font-weight: 700; margin-bottom: 5px;">病历号: {{ caseInfo.caseCode6 }}</div>
  <div style="font-size: 14px; font-weight: 700;">问诊人: {{ caseInfo.caseUname }}</div>
</div>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <!-- <el-form-item label="治疗编号" prop="treatCode8">
          <el-input v-model="form.treatCode8" placeholder="请输入治疗编号" />
        </el-form-item> -->
        <!-- <el-form-item label="治疗了问诊人" prop="treatOpenid">
          <el-input v-model="form.treatOpenid" placeholder="请输入治疗了问诊人" />
        </el-form-item> -->
        <el-form-item label="治疗详情">
          <editor v-model="form.treatText" :min-height="192" />
        </el-form-item>
        <el-form-item label="治疗备注" prop="treatRemark">
          <el-input v-model="form.treatRemark" placeholder="请输入治疗备注" />
        </el-form-item>
        <!-- <el-form-item label="关联案例id" prop="treatCaseid">
          <el-input v-model="form.treatCaseid" placeholder="请输入关联案例id" />
        </el-form-item>
        <el-form-item label="关联病uuid号" prop="treatCaseuuid">
          <el-input v-model="form.treatCaseuuid" placeholder="请输入关联病uuid号" />
        </el-form-item>
        <el-form-item label="医生id" prop="treatDoctorid">
          <el-input v-model="form.treatDoctorid" placeholder="请输入医生id" />
        </el-form-item> -->
        <el-form-item label="医生签名" prop="treatDoctorname">
          <el-input v-model="form.treatDoctorname" placeholder="请输入医生签名" />
        </el-form-item>
        <!-- <el-form-item label="创建时间" prop="treatCreatetime">
          <el-date-picker clearable v-model="form.treatCreatetime" type="date" value-format="yyyy-MM-dd"
            placeholder="请选择创建时间">
          </el-date-picker>
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listTreat, getTreat, delTreat, addTreat, updateTreat } from "@/api/ask/treat";

export default {
  name: "Treat",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 治疗管理表格数据
      treatList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      modalShown: false,

      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        treatCode8: null,
        treatOpenid: null,
        treatText: null,
        treatRemark: null,
        treatCaseid: null,
        treatDoctorid: null,
        treatDoctorname: null,
        treatCreatetime: null,
      },
      caseInfo: {},
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        treatCreatetime: [
          { required: true, message: "创建时间不能为空", trigger: "blur" }
        ],
        treatUpdatetime: [
          { required: true, message: "更新时间不能为空", trigger: "blur" }
        ]
      }
    };
  },
  watch: {
    '$route': 'setModalState',
  },
  created() {
    this.getList();

  },
  mounted() {
    this.setModalState();

  },
  methods: {
    /** 查询治疗管理列表 */
    getList() {
      this.loading = true;
      this.queryParams.treatCaseid = this.$route.query.caseId
      this.caseInfo = this.$route.query
      console.log(this.caseInfo, "caseInfoXXXXXXXXXXXXXXXXXXXXXXXX")
      listTreat(this.queryParams).then(response => {
        this.treatList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    setModalState() {
      if (this.$route.query.caseId != null) {
        // let open = localStorage.getItem('modalShown')
        if (open) {
          this.getList();
          this.reset();
          // this.open = true;
          // this.title = "添加治疗管理"
          // this.form.treatCaseid = this.$route.query.caseId
          // localStorage.removeItem('modalShown');
        }
      }

    },
    // 表单重置
    reset() {
      this.form = {
        treatId: null,
        treatCode8: null,
        treatOpenid: null,
        treatText: null,
        treatRemark: null,
        treatCaseid: null,
        treatCaseuuid: null,
        treatDoctorid: null,
        treatDoctorname: null,
        treatFrontpoint: null,
        treatBackpoint: null,
        treatCreatetime: null,
        treatUpdatetime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.treatId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.form.treatCaseid = this.$route.query.caseId
      this.form.treatCaseuuid = this.$route.query.caseUuid
      this.form.treatOpenid =  this.$route.query.caseOpenid
      this.title = "添加治疗管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const treatId = row.treatId || this.ids
      getTreat(treatId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改治疗管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.treatId != null) {
            // 如果是修改
            updateTreat(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            // 如果是新增
            addTreat(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
              this.$confirm('是否继续新增', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
              }).then(() => {
                this.handleAdd();
                this.form.treatCaseid = this.$route.query.caseId
              }).catch(() => {
                //取消重置内容信息,且弹窗关闭
                this.reset();
                this.open = false;
              });
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const treatIds = row.treatId || this.ids;
      this.$modal.confirm('是否确认删除治疗管理编号为"' + treatIds + '"的数据项？').then(function () {
        return delTreat(treatIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('ask/treat/export', {
        ...this.queryParams
      }, `treat_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
